/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   SpotInspectionServiceImpl.java for com.pengtu.gsj.service.impl.check.zhgl.siteCheck    </p>
 * 
 * <p>最后修改 : $: 2017年8月14日-下午3:13:51 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.zhgl.siteCheck;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.zhgl.SiteCheckDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.zhgl.SiteCheck;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.zhgl.siteCheck.SpotInspectionService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;
import com.pengtu.gsj.utils.StringUtils;

/**
 * 
 * SpotInspectionServiceImpl
 * 
 * 2017年8月14日 下午3:13:51
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class SpotInspectionServiceImpl implements SpotInspectionService{
	
	@Autowired
	private SiteCheckDao siteCheckDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	
	@Override
	public List<SiteCheck> getSiteCheckList(QueryFilter queryFilter){
		return siteCheckDao.find(queryFilter, "testYear", true);
	}
	
	@Override
	public Map<String, Object> getSiteCheckMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<SiteCheck> siteCheckList = new ArrayList<SiteCheck>();
		List<Object> values = new ArrayList<Object>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		double reserveMark = Double.parseDouble((String)jsonObject.get("reserveMark"));
		double firstStepScore = Double.parseDouble((String)jsonObject.get("firstStepScore"));
		double secondStepScore = Double.parseDouble((String)jsonObject.get("secondStepScore"));
		double thirdStepScore = Double.parseDouble((String)jsonObject.get("thirdStepScore"));
		double fourthStepScore = Double.parseDouble((String)jsonObject.get("fourthStepScore"));
		
		String roadId = (String)jsonObject.get("roadNameId");
		//取到所有文件
		List<Attachment> list = systemManageService.getCheckAttachmentsByPartId(startTime, endTime, tag, roadId);
		
		//现场检查路线的名称
		QueryFilter queryFilter = new QueryFilter();
		//年度养护资金详情查询
		queryFilter.addFilter("testYear", startTime, MatchType.GE);
		queryFilter.addFilter("testYear", endTime, MatchType.LE);
		
		double total =0; 		//检查得分
		double percentage =0; 	//系统完善度
		double tableFull  = MathsUtils.sumDouble(MathsUtils.sumDouble(yearFull, poitMark), MathsUtils.sumDouble(firstStepScore, thirdStepScore));	//满分
		
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 0)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		
		if (StringUtils.isEmpty(roadId)) {
			siteCheckList = this.getSiteCheckList(queryFilter);
		} else {
			values.add(roadId);
			
			queryFilter.addFilter("id", roadId, MatchType.EQ);
			siteCheckList = this.getSiteCheckList(queryFilter);
			
			for (SiteCheck siteCheck : siteCheckList) {
				//路面情况
				if (siteCheck.getRoadCondition().equals("1")) {
					total = MathsUtils.sumDouble(total, yearFull);
				} else if (siteCheck.getRoadCondition().equals("2")) {
					total = MathsUtils.sumDouble(total, yearPart);
				}
				
				//公路设施状况
				if (siteCheck.getRoadFacility().equals("1")) {
					total = MathsUtils.sumDouble(total, poitMark);
				} else if (siteCheck.getRoadFacility().equals("2")) {
					total = MathsUtils.sumDouble(total, reserveMark);
				}
				
				//路基，边坡状况
				if (siteCheck.getRoadBed().equals("1")) {
					total = MathsUtils.sumDouble(total, firstStepScore);
				} else if (siteCheck.getRoadBed().equals("2")) {
					total = MathsUtils.sumDouble(total, secondStepScore);
				}
				
				//绿化及管养情况
				if (siteCheck.getRoadAfforest().equals("1")) {
					total = MathsUtils.sumDouble(total, thirdStepScore);
				} else if (siteCheck.getRoadAfforest().equals("2")) {
					total = MathsUtils.sumDouble(total, fourthStepScore);
				}
				
				if (total > tableFull) {
					total = tableFull;
				}
			}
			
			map.put("sum",  MathsUtils.formatDouble(total,1));
			//判断完善度
			percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
			map.put("percent", percentage);
			map.put("years", years);
		}
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("att", list);
		map.put("overView", siteCheckList);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
